package com.solution.wx.bizman.home.controller;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.solution.admin.message.constants.MessageConstants.MessageType;
import com.solution.admin.message.entity.MessageEntity;
import com.solution.admin.message.service.MessageService;
import com.solution.admin.order.constants.OrderConstants.ORDER_STATUS;
import com.solution.admin.order.service.OrderService;
import com.solution.admin.report.service.CustomerOrderDailyDataService;
import com.solution.common.controller.BaseController;
import com.solution.common.utils.DateUtils_add;
import com.solution.wx.common.constants.PublicConstants;

/**
 * 微信商户端首页控制层
 * @author: solin
 * @date: 2017年12月27日 下午2:05:25
 */
@RequestMapping("/wx/bizman")
@Controller("wxBizmanIndexController")
public class IndexController extends BaseController{
	
	@Autowired
	private CustomerOrderDailyDataService customerOrderDailyDataService;
	
	@Autowired
	private OrderService orderService;
	@Autowired
	private MessageService messageService;
	
	/**
	 * 微信商户端首页
	 * @param request
	 * @return
	 * @author: solin
	 * @date: 2017年12月27日 下午2:09:13
	 */
	@RequestMapping("/index")
	public String toListSiteModulePage(HttpServletRequest request) {
		request.setAttribute(PublicConstants.CURRENT_PAGE_KEY, PublicConstants.PAGE_HOME);
		
		try {
			/** 一、获取消息、资讯 */
			MessageEntity messageSys = null;
			MessageEntity messageDyn = null;
			Long userId = getUserId();
			// 系统消息
			List<MessageEntity> messageSystemList = messageService.getWxMessageList(MessageType.TYPE_SYSTEM, userId, 1);
			// 业界动态消息
			List<MessageEntity> messageDynamicList = messageService.getWxMessageList(MessageType.TYPE_DYNAMIC, userId, 1);
			if (null != messageSystemList && messageSystemList.size() > 0) {
				messageSys = messageSystemList.get(0);
			}
			if (null != messageDynamicList && messageDynamicList.size() > 0) {
				messageDyn = messageDynamicList.get(0);
			}
			// 首先加入
			if (null != messageSys) {
				request.setAttribute("message", messageSys);
			}
			if (null != messageSys && null != messageDyn) {
				// 添加时间较新的
				if (messageSys.getModifyTime().after(messageDyn.getModifyTime())) {
					request.setAttribute("message", messageSys);
				} else {
					request.setAttribute("message", messageDyn);
				}
			}
			/** 二：计算数量 */
			Calendar cal = Calendar.getInstance();
			cal.set(Calendar.DAY_OF_MONTH, 1);
			//下个月首日
			String nextMonth = DateUtils_add.formatDate(cal.getTime(), DateUtils_add.DATE_FORMAT_DATEONLY);
			//本月首日
			cal.add(Calendar.MONTH, -1);
			String thisMonth = DateUtils_add.formatDate(cal.getTime(), DateUtils_add.DATE_FORMAT_DATEONLY);
			Integer orderCount = customerOrderDailyDataService.getPresentMonthOrderCount(thisMonth, nextMonth);
			
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("userId", getUserId());
			Integer userOrderCount = orderService.countSalesmanOrderSum(paramMap);
			paramMap.put("orderStatus", ORDER_STATUS.WAIT_TO_APPROVE.getCode());
			Integer waitToApprove = orderService.countSalesmanOrderSum(paramMap);
			request.setAttribute("orderCount", orderCount);
			request.setAttribute("salesmanOrderCount", userOrderCount);
			request.setAttribute("salesmanWaitToApprove", waitToApprove);
		} catch (Exception e) {
			List<MessageEntity> messageList = messageService.listMessage(null);
			if (null != messageList && messageList.size() > 0) {
				request.setAttribute("message", messageList.get(0));
			}
			e.printStackTrace();
		}
		return "wx/bizman/home/index";
	}
}

